import { defineStore } from 'pinia';

import { Locale } from '@nutui/nutui';
import enUS from '@nutui/nutui/dist/packages/locale/lang/en-US';
import zhCh from '@nutui/nutui/dist/packages/locale/lang/zh-CN';
import viVN from '@/languages/lang/nutui/vi-VN';
import { dingZhiActivity } from '@/enums/activityEnum';
import { splitArrayIntoChunks } from '@/utils/publicMethods';
/**
 * 页面信息
 * @methods setUserInfos 设置用户信息
 */
interface PageState {
    menuActive: number
    countryOptions: any
    bannerArr: any
    textAnnouncement: any
    serviceUrlObj: any
    settings: any,
    gameIsLoading: boolean,
    adminI18n: any
    lang: string,
    unread: number,
    homeGameData: any,
    activityList: any,
    activityTitleList: any,
    showLeft: boolean,
    cubeGames: any,
    hotBoxGames: any,
    defaultActivityList: any

}
export const Page = defineStore('pages', {

    state: (): PageState => ({
        menuActive: -1,
        countryOptions: null,
        bannerArr: [],
        textAnnouncement: [],
        serviceUrlObj: {},
        settings: null,
        homeGameData: null,
        gameIsLoading: false,
        adminI18n: null,
        lang: navigator.language || 'zh-CN',
        unread: 0,
        defaultActivityList: null,
        activityList: null,
        activityTitleList: null,
        showLeft: false,
        cubeGames: [],   // HomeGameCube 的游戏数据
        hotBoxGames: [], // HomeGameHotBox 的游戏数据
    }),
    actions: {
        // 获取未读消息数量
        async getUnread(isFirst = false) {
            if (isFirst) {
                this.unread = 0
            }
            const customer_server: any = this.$state.settings?.customer_server
            let device_id
            if (localStorage.getItem('deviceid')) {
                device_id = localStorage.getItem('deviceid') || ''
            }
            //  device_id = localStorage.getItem('deviceid')
            console.log('未读id', 'url', device_id, customer_server + `/api/message/unread?device_id=${device_id}`)
            if (customer_server && device_id) {
                fetch(customer_server + `/api/message/unread?device_id=${device_id}`)
                    .then(response => {
                        if (!response.ok) {
                            throw new Error('Network response was not ok ' + response.statusText);
                        }
                        return response.json(); // 将响应解析为 JSON
                    })
                    .then(res => {
                        console.log('unread', res?.data)
                        this.unread = res?.data?.count || 0
                    })
            }
        },

        async setHomePageGameData(data: any) {

            data.map((e: any) => {
                if (e.id == 0) {
                    e.newList = splitArrayIntoChunks(e.three_platform, 8)
                } else {
                    e.newList = splitArrayIntoChunks(e.three_platform, 6)
                }
            })
            this.homeGameData = data
        },
        async setSettings(value: any) {
            this.settings = value
        },
        async setAdminI18n(value: any) {
            this.adminI18n = value
        },
        async setGameIsLoading(value: any) {
            this.gameIsLoading = value
        },
        async setLang(value: any) {
            this.lang = ''
            this.serviceUrlObj = null
            this.bannerArr = null
            // this.textAnnouncement = null
            let langObj: any = {
                'en-US': enUS,
                'zh-CN': zhCh,
                'vi-VN': viVN
            }
            if (value) {
                this.lang = value
            }
            Locale.use(this.lang, langObj[this.lang]);
            let strs = this.lang.split('-')
            let str = strs[0]
            if (strs[0] == 'vi') {
                str = 'vn'
            }
            let keys = Object.keys(this.adminI18n[str])
            let bannerArr: Array<string> = []
            // let textAnnouncement: Array<string> = []
            let serviceUrlObj: any = {}
            keys.map((e: string) => {
                if (e.indexOf('pc') == -1 && e.indexOf('admin_banner_list') != -1) {
                    bannerArr.push(e)
                }

                // if (e.indexOf('system_notice_content') != -1) {
                //     textAnnouncement.push(e)
                // }
                if (e.indexOf('service_') != -1) {
                    serviceUrlObj[e] = this.adminI18n[str][e]
                }
            })
            this.serviceUrlObj = serviceUrlObj
            this.bannerArr = bannerArr
            // this.textAnnouncement = textAnnouncement
            // console.log(666666666,this.textAnnouncement)
            this.setActivityTitleList(this.defaultActivityList)
        },

        async setTextAnnouncementMore(str: string) {
            this.textAnnouncement.push(str)
            this.textAnnouncement.splice(0, 0)
        },
        async setActivityTitleList(value: any) {
            if (!value) {
                return
            }
            this.defaultActivityList = value
            let langObj: any = {
                'en-US': 'en',
                'zh-CN': 'zh',
                'vi-VN': 'vn'
            }
            let lang = langObj[this.lang] || this.lang

            let keys = Object.keys(this.adminI18n[lang]);
            // 定制活动id
            let dingzhiId = [10000, 9000, 9010, 10005, 9020, 8001];
            let newList = value.filter(
                (item: any) =>
                    keys.includes(item.pic_link) || dingzhiId.includes(item.id),
            );
            this.activityList = value.filter(
                (item: any) =>
                    keys.includes(item.pic_link)
            );

            this.activityList = splitArrayIntoChunks(this.activityList, 6)

            newList = newList.map((item: any) => ({
                name:
                    dingZhiActivity[item.id]?.name ??
                    this.adminI18n[lang][item.name],
                pic_link:
                    dingZhiActivity[item.id]?.pic_link ??
                    this.adminI18n[lang][item.pic_link],
                details: this.adminI18n[lang][item.details] || '',
                content: this.adminI18n[lang][item.content] || '',
                pic_content: this.adminI18n[lang][item?.pic_link.replace("title", "content")],
                rules: this.adminI18n[lang][item.rules] || '',
                total: item.total || '',
                id: item.id,
                tag: dingZhiActivity[item.id]?.tag ?? item.tag,
            }));

            if (newList.length > 0) {
                let obj: any = {
                    home_page_all: newList,
                };
                newList.map((e: any) => {
                    obj[e.tag] = [];
                    newList.map((j: any) => {
                        if (j.tag == e.tag) {
                            obj[e.tag].push(j);
                        }
                    });
                });
                this.activityTitleList = obj;
            }

        },
        async updateCubeGames(data: any[]) {
            this.cubeGames = data;
        },
        async updateHotBoxGames(data: any[]) {
            this.hotBoxGames = data;
        },

    }
});
